import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hsh/style/style.dart';
import 'package:flutter_hsh/widgets/keep_alive_wrapper.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import '../../fluttergromore/ads/ads_utils.dart';
import '../../fluttergromore/utils/dialog_utils.dart';

import '../Home/health_score_view.dart';
import '../jieqi/jieqi_view.dart';
import '../jilu/jilu_view.dart';
import '../reliang/reliang_view.dart';
import '../mine/mine_view.dart';
import 'main_logic.dart';
import 'navi_model.dart';

class MainPage extends GetView<MainLogic> {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: Scaffold(
        backgroundColor: kAppBcgColor,
        resizeToAvoidBottomInset: false,
        body: WillPopScope(
          onWillPop: () async {
            DialogUtils.showBack(context);
            return false;
          },
          child: GetBuilder<MainLogic>(
            builder: (logic) {
              return IndexedStack(
                index: logic.selectedIndex,
                children: [
                  const KeepAliveWrapper(child: HealthScoreView()),
                  const KeepAliveWrapper(child: JieqiPage()),
                  const KeepAliveWrapper(child: JiluPage()),
                  KeepAliveWrapper(child: ReliangPage()),
                  const KeepAliveWrapper(child: MinePage()),
                ],
              );
            },
            id: idPage,
          ),
        ),
        extendBody: false,
        bottomNavigationBar: GetBuilder<MainLogic>(
          builder: (logic) {
            List<Widget> items =
                List.generate(logic.bottomList.length, (index) {
              bool selected = logic.selectedIndex == index;
              NaviModel model = logic.bottomList[index];
              return Expanded(
                child: CupertinoButton(
                  minSize: 0,
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SvgPicture.asset(
                        model.icon ?? '',
                        width: 27,
                        height: 27,
                        colorFilter: ColorFilter.mode(
                          selected ? const Color(0xFF4A6EE0) : const Color(0xFF666666),
                          BlendMode.srcIn,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        model.title ?? '',
                        style: TextStyle(
                          fontSize: 12,
                          height: 1.17,
                          fontWeight: selected ? FontWeight.w700 : FontWeight.w400,
                          color: selected ? const Color(0xFF4A6EE0) : const Color(0xFF666666),
                        ),
                      ),
                    ],
                  ),
                  onPressed: () {

                    logic.switchTab(index);
                  },
                ),
              );
            });

            return Container(
              padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
              decoration: const BoxDecoration(
                  color: Color(0xFFFFFFFF),
                  boxShadow: [
                    BoxShadow(
                      color: Color(0x26A2785C),
                      offset: Offset(0, -2),
                      blurRadius: 10,
                      spreadRadius: 0,
                    ),
                  ]),
              child: SafeArea(
                child: Row(children: items),
              ),
            );
          },
          id: idBottomBar,
        ),
      ),
    );
  }
}
